package com.ruoyi.car.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.car.controller.vo.FileUploadVo;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.car.domain.CarFile;
import com.ruoyi.car.service.ICarFileService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆文件Controller
 *
 * @author liujinlei
 * @date 2022-01-15
 */
@RestController
@RequestMapping("/file")
public class CarFileController extends BaseController {
    @Autowired
    private ICarFileService carFileService;

    /**
     * 查询车辆文件列表
     */
    @RequiresPermissions("car:file:list")
    @GetMapping("/list")
    public TableDataInfo list(CarFile carFile) {
        startPage();
        List<CarFile> list = carFileService.selectCarFileList(carFile);
        return getDataTable(list);
    }

    /**
     * 导出车辆文件列表
     */
    @RequiresPermissions("car:file:export")
    @Log(title = "车辆文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarFile carFile) {
        List<CarFile> list = carFileService.selectCarFileList(carFile);
        ExcelUtil<CarFile> util = new ExcelUtil<CarFile>(CarFile.class);
        util.exportExcel(response, list, "车辆文件数据");
    }

    /**
     * 获取车辆文件详细信息
     */
    @RequiresPermissions("car:file:query")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId) {
        return AjaxResult.success(carFileService.selectCarFileByFileId(fileId));
    }

    /**
     * 新增车辆文件
     */
    @RequiresPermissions("car:file:add")
    @Log(title = "车辆文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarFile carFile) {
        return toAjax(carFileService.insertCarFile(carFile));
    }

    /**
     * 修改车辆文件
     */
    @RequiresPermissions("car:file:edit")
    @Log(title = "车辆文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarFile carFile) {
        return toAjax(carFileService.updateCarFile(carFile));
    }

    /**
     * 删除车辆文件
     */
    @RequiresPermissions("car:file:remove")
    @Log(title = "车辆文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds) {
        return toAjax(carFileService.deleteCarFileByFileIds(fileIds));
    }

    /**
     * 上传图片
     */
    @PostMapping("upload")
    public AjaxResult upload(FileUploadVo busType) {
        return toAjax(true);
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, FileInfo fileInfo) throws IOException {
        logger.info(file.getOriginalFilename());
        logger.info(file.getName());
        // 上传文件路径
//        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
//        String fileName = FileUploadUtils.upload(filePath, file);
//        fileInfo.setFilePath(fileName);
        return toAjax(1);
    }
}
